package com.bridgeintelligent.tag.webserver.dim.mgmt.service.mq;

import cn.hutool.core.util.RandomUtil;
import com.bridgeintelligent.tag.constants.bulkload.BulkLoadMqMsg;
import com.bridgeintelligent.tag.constants.bulkload.FileToHdfsMsg;
import com.bridgeintelligent.tag.constants.bulkload.MQConfig;
import com.bridgeintelligent.tag.mapper.DimItemMapper;
import com.bridgeintelligent.tag.mapper.DimMapper;
import com.bridgeintelligent.tag.rocketmq.RocketMqConfig;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimData;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * Created by JackCheung on 2019/9/22 10:53 PM.
 */
@AllArgsConstructor
@Component
@Slf4j
public class SendMsgHandler {

    private RocketMQTemplate rocketMQTemplate;
    private ObjectMapper objectMapper;
    private DimItemMapper dimItemMapper;
    private DimMapper dimMapper;
    RocketMqConfig rocketMqConfig;

    /**
     * see {@link BulkLoadMqMsg}
     */
    public void sendMsg(String dimId, int type) {
        DimData dimData = dimMapper.detail(dimId);
        String filePath = (type == 1 || type == 2) ? dimData.getPartCustList() : dimData.getCustList();
        String originalFile = dimData.getCustList();
//        List<DimItem> list = dimItemMapper.list(dimId);
//        if (CollectionUtils.isEmpty(list)) {
//            throw new DimException(new ExceptionCode(999, "此自定义标签无标签项！"));
//        }
        BulkLoadMqMsg bulkLoadMqMsg = new BulkLoadMqMsg(dimId, dimData.getTheme(), filePath, originalFile, "", type, null, dimData.getClassify(), dimData.isWorking());
        try {
            String msg = objectMapper.writeValueAsString(bulkLoadMqMsg);
            log.info("\n发送消息------->>>>"+msg);
            rocketMQTemplate.convertAndSend(MQConfig.REAL_TIME_TAG_TOPIC, msg);
        } catch (JsonProcessingException e) {
            log.error(dimId+"--------》》》定制标签发送MQ失败");
        }
    }

    public void sendFileBakUpMsg(String filePath) {
//        DimData dimData = dimMapper.detail(dimId);
        if(StringUtils.isEmpty(filePath)){
            return;
        }
        FileToHdfsMsg hdfsMsg = new FileToHdfsMsg();
        hdfsMsg.setFilePath(filePath);//上传文件全路径
        hdfsMsg.setType(1);//1:定制标签文件
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator)+1);
        String id = fileName.substring(0,fileName.lastIndexOf("."));
        hdfsMsg.setUniqueIdentify(id+"_"+ RandomUtil.randomString(5));
        try {
            String msg = objectMapper.writeValueAsString(hdfsMsg);
            log.info("=====>文件备份发送消息:{}",msg);
            rocketMQTemplate.convertAndSend(MQConfig.FILE_BAKUP_HDFS_TOPIC, msg);
        } catch (JsonProcessingException e) {
            log.error("=====>[{}]文件备份发送MQ失败!!!",filePath);
        }
    }
}
